var RoleModule = (function() {

    var boolRender = function(v, r, i) {
        var access = v == '1' ? 'Yes' : 'No';
        return access;
    };

    function handleGrid() {

        $('#grid').datagrid({
            url: siteURL('role_module/load_data'),
            title: 'Role Module',
            height: 400,
            rownumbers: true,
            pagination: true,
            pagePosition: 'top',
            autoRowHeight: false,
            checkbox: true,
            singleSelect: true,
            columns: [
                [{
                    field: 'ck',
                    checkbox: true
                }, {
                    field: 'group_name',
                    title: 'Group Name',
                    width: 180,
                    sortable: true
                }, {
                    field: 'module_name',
                    title: 'Module Name',
                    width: 180,
                    sortable: true
                }, {
                    field: 'access',
                    title: 'Access',
                    width: 180,
                    sortable: true,
                    formatter: boolRender
                }, ]
            ]
        });

        var pager = $('#grid').datagrid('getPager');
        pager.pagination({
            buttons: [{
                text: '<i class="icon-plus"></i> Add',
                handler: addRecord
            }, {
                text: '<i class="icon-pencil"></i> Edit',
                handler: editRecord
            }, {
                text: '<i class="icon-trash"></i> Delete',
                handler: deleteRecord
            }]
        });

    }

    function handleWindow() {
        $('#win').show();
        $('#win').dialog({
            modal: true,
            width: 725,
            title: 'Editor',
            buttons: [{
                text: 'Submit',
                handler: function() {
                    $('#form').form('submit', {
                        url: siteURL('role_module/save_data'),
                        success: function() {
                            $('#win').dialog('close');
                            $('#grid').datagrid('reload');
                        }
                    });
                }
            }, {
                text: 'Close',
                handler: function() {
                    $('#win').dialog('close');
                }
            }]
        });
        $('#win').dialog('close');

        $('form').submit(function(e) {
            e.preventDefault();
        });
    }

    function addRecord() {
        $('#win').dialog('open');
        $('#form').form('clear').form('reset');
    }

    function editRecord() {
        var record = $('#grid').datagrid('getSelected');
        if (!record) {
            $.messager.alert('Warning', 'No record selected', 'warning');
            return;
        }

        $('#win').dialog('open');
        $('#form').form('load', record);
    }

    function deleteRecord() {
        var record = $('#grid').datagrid('getSelected');
        if (!record) {
            $.messager.alert('Warning', 'No record selected', 'warning');
            return;
        }

        $.messager.confirm('Confirm', 'Delete selected record?', function(r) {
            if (r) {
                $.ajax({
                        url: siteURL('role_module/delete_data'),
                        type: 'post',
                        dataType: 'json',
                        data: record
                    })
                    .done(function() {
                        $('#grid').datagrid('reload');
                    });
            }
        });
    }

    function handleSearch() {
        $('#omnisearch').on('keypress', function(e) {
            if (e.keyCode === 13) {
                var query = $(this).val();
                $('#grid').datagrid('load', {
                    query: query
                });
            }
        });
    }

    return {
        init: function() {
            handleGrid();
            handleWindow();
            handleSearch();
        }
    };

}(jQuery));

$(document).ready(RoleModule.init);
